//
//  MainTabBarVC.m
//  ShanghaiShopping
//
//  Created by KDC on 2019/8/22.
//  Copyright © 2019 KDC. All rights reserved.
//

#import "MainTabBarVC.h"

#import "HomeViewController.h"
#import "MapViewController.h"
#import "DiscoverViewController.h"
#import "MineViewController.h"

@interface MainTabBarVC () <UITabBarControllerDelegate>
@property (nonatomic, strong) HomeViewController *vcHome;
@property (nonatomic, strong) MapViewController *vcMap;
@property (nonatomic, strong) DiscoverViewController *vcDiscover;
@property (nonatomic, strong) MineViewController *vcMine;

@property (nonatomic, strong) LoginAndRegisterVC *vcTest;

@end

@implementation MainTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpViewController];
    self.delegate = self;
}

- (void)setUpViewController {
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:Font_Tabbar_Title,
                                                        NSForegroundColorAttributeName:Color_Tabbar_Normal}
                                             forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:Font_Tabbar_Title,
                                                        NSForegroundColorAttributeName:Color_Tabbar_Selected}
                                             forState:UIControlStateSelected];
    
    _vcHome = [[HomeViewController alloc] init];
    UINavigationController *nav0 = [self createNavigationControllerWithRootViewController:_vcHome
                                                                                    title:@"首页"
                                                                                    image:ImageNamed(@"TabBar_N0")
                                                                            selectedImage:ImageNamed(@"TabBar_Y0")
                                                                                      tag:800];
    _vcMap = [[MapViewController alloc] init];
    UINavigationController *nav1 = [self createNavigationControllerWithRootViewController:_vcMap title:@"地图"
                                                                                    image:ImageNamed(@"TabBar_N1")
                                                                            selectedImage:ImageNamed(@"TabBar_Y1")
                                                                                      tag:801];
    _vcDiscover = [[DiscoverViewController alloc] init];
    UINavigationController *nav2 = [self createNavigationControllerWithRootViewController:_vcDiscover title:@"发现"
                                                                                    image:ImageNamed(@"TabBar_N2")
                                                                            selectedImage:ImageNamed(@"TabBar_Y2")
                                                                                      tag:802];
    _vcMine = [[MineViewController alloc] init];
    UINavigationController *nav3 = [self createNavigationControllerWithRootViewController:_vcMine title:@"我的"
                                                                                    image:ImageNamed(@"TabBar_N3")
                                                                            selectedImage:ImageNamed(@"TabBar_Y3")
                                                                                      tag:803];
    
    
//    _vcTest = [[LoginAndRegisterVC alloc] init];
//    UINavigationController *navTest = [self createNavigationControllerWithRootViewController:_vcTest title:@"Test"
//                                                                                       image:ImageNamed(@"TabBar_N3")
//                                                                               selectedImage:ImageNamed(@"TabBar_Y3")
//                                                                                         tag:804];
    
//    self.tabBar.translucent = NO;
    self.viewControllers = @[
                             nav0,
                             nav1,
                             nav2,
                             nav3
//                             ,
//                             navTest
                             ];
}

- (UINavigationController *)createNavigationControllerWithRootViewController:(UIViewController *)viewController title:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage tag:(NSInteger)tag {
    
    UINavigationController *result = [[UINavigationController alloc] initWithRootViewController:viewController];
    result.tabBarItem.title = title;
    result.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    result.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    result.tabBarItem.tag = tag;
    return result;
}
#pragma mark -- tabBarController delegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return YES;
}

//获取当前显示在最前面的页面的vc
- (id)getCurrentViewController {
    UINavigationController *navNow = [self.viewControllers objectAtIndex:self.selectedIndex];
    return [navNow.viewControllers objectAtIndex:[navNow.viewControllers count] - 1];
}

@end
